package com.conferences.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.conferences.dao.UserDAO;
import com.conferences.model.User;

@Repository("userDAO")
public class UserDAOImpl extends GenericHibernateDao<User> implements UserDAO {

	@Transactional(readOnly = true)
	@Override
	public User findByUsername(String username) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("username", username);
		List<User> users = findByQuery("from User where username=:username",
				params);
		return users.size() > 0 ? users.get(0) : null;
	}

	@Override
	public void save(User user) {
		super.save(user);
	}
	
	@Transactional(readOnly = true)
	@Override
	public User get(Long id) {
		Criteria crit = getSession().createCriteria(User.class);
		crit.add(Restrictions.eq("id", id));
		return (User) crit.uniqueResult();
	}
}
